package cn.concurrent.notify;

import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Author: lz
 * @Date: 2018/9/3 17:22
 * @Version 1.0
 */
public class ProductorConsumerByCondition {

	private static ReentrantLock lock = new ReentrantLock();
	private static Condition full = lock.newCondition();
	private static Condition empty = lock.newCondition();

	public static void main(String[] args) {
		LinkedList linkedList = new LinkedList();
		ExecutorService service = Executors.newFixedThreadPool(15);
		for (int i = 0; i < 5; i++) {
			service.submit(new Productor(linkedList, 8, lock));
		}
		for (int i = 0; i < 10; i++) {
			service.submit(new Consumer(linkedList, lock));
		}

	}

	static class Productor implements Runnable {

		private List<Integer> list;
		private int maxLength;
		private Lock lock;

		public Productor(List list, int maxLength, Lock lock) {
			this.list = list;
			this.maxLength = maxLength;
			this.lock = lock;
		}

		@Override
		public void run() {
			while (true) {
				lock.lock();
				try {
					while (list.size() == maxLength) {
						System.out.println("生产者" + Thread.currentThread().getName() + "  list以达到最大容量，进行wait");
						full.await();
						System.out.println("生产者" + Thread.currentThread().getName() + "  退出wait");
					}
					Random random = new Random();
					int i = random.nextInt();
					System.out.println("生产者" + Thread.currentThread().getName() + " 生产数据" + i);
					list.add(i);
					empty.signalAll();
				} catch (InterruptedException e) {
					e.printStackTrace();
				} finally {
					lock.unlock();
				}
			}
		}
	}


	static class Consumer implements Runnable {

		private List<Integer> list;
		private Lock lock;

		public Consumer(List list, Lock lock) {
			this.list = list;
			this.lock = lock;
		}

		@Override
		public void run() {
			while (true) {
				lock.lock();
				try {
					while (list.isEmpty()) {
						System.out.println("消费者" + Thread.currentThread().getName() + "  list为空，进行wait");
						empty.await();
						System.out.println("消费者" + Thread.currentThread().getName() + "  退出wait");
					}
					Integer element = list.remove(0);
					System.out.println("消费者" + Thread.currentThread().getName() + "  消费数据：" + element);
					full.signalAll();
				} catch (InterruptedException e) {
					e.printStackTrace();
				} finally {
					lock.unlock();
				}
			}
		}
	}

}

